﻿class com.criticalmass.common.drawing.lines.DashedLine {
	
	private var moveTo:Function;
	private var lineTo:Function;
	private var lineStyle:Function;
/**
* This method draws dashed and dotted lines. It is an extension of the MovieClip Class
* @param startx the starting x postion of the line
* @param starty the starting y postion of the line
* @param endx the ending x postion of the line
* @param endy the ending y postion of the line
* @param len the length of each segment
* @param gap the gap between segments
* @param style (optional) a lineStyle object to use for the line consisting of thickness, col, and alpha
*/
	function drawDashedLine(startx:Number, starty:Number, endx:Number, endy:Number, len:Number, gap:Number, style:Object):Void {
		var segLength = len+gap;
		var dx = endx - startx;
		var dy = endy - starty;
		var delta = Math.sqrt((dx * dx) + (dy * dy));
		var segs = Math.floor(Math.abs(delta / segLength));
		var radians = Math.atan2(dy, dx);
		var sx = startx;
		var sy = starty;
		var cx = Math.cos(radians)*segLength;
		var cy = Math.sin(radians)*segLength;
		
		if (style != undefined) {
			this.lineStyle(style.thickness, style.col, style.alpha);
		}
		for (var i=0; i<segs; i++) {
			this.moveTo(sx, sy);
			this.lineTo(sx+Math.cos(radians)*len, sy+Math.sin(radians)*len);
			sx += cx;
			sy += cy;
		}
		this.moveTo(sx, sy);
		delta = Math.sqrt((endx-sx)* (endx-sx)+(endy-sy)*(endy-sy));
		if (delta > len) {
			this.lineTo(sx+Math.cos(radians)*len, sy+Math.sin(radians)*len);
		} else if (delta > 0) {
			this.lineTo(sx+Math.cos(radians)*delta, sy+Math.sin(radians)*delta);
		}
		this.moveTo(endx, endy);
	}
/**
* The classConstructed method ensures that the MovieClip Class is extended with this new method
*/
	static function classConstruct(Void):Boolean {
		MovieClip.prototype.drawDashedLine = DashedLine.prototype.drawDashedLine;
		return true;
	}
	static var classConstructed = classConstruct();
}